package es.cazadoresmadridejos.beans.controller.biggame;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import es.cazadoresmadridejos.beans.manager.partner.PartnerManager;
import es.cazadoresmadridejos.beans.manager.temps.TempsManager;
import es.cazadoresmadridejos.model.Temps;
import es.cazadoresmadridejos.objs.form.OrderForm;
import es.cazadoresmadridejos.objs.tablrecords.BigGameOrderTableRecord;
import es.cazadoresmadridejos.util.constants.Constants;

@Controller
public class BigGameOrderController {

//	@Autowired private BigGameManager bigGameManager;
	@Autowired private PartnerManager partnerManager;
	@Autowired private TempsManager tempsManager;
	
	@RequestMapping(value="/bigGameOrder.html")
	public ModelAndView order(){
		ModelAndView mv = new ModelAndView();
		
		List<BigGameOrderTableRecord> partners = partnerManager.findAllByOrder();
	
		mv.addObject("partners", partners);
		mv.addObject("orderForm", new OrderForm());
		mv.addObject("currentOrder", tempsManager.getByKey(Temps.BIG_GAME_ORDER).getNumeric());
		
		return mv;
	}
	
	@RequestMapping(value="/bigGameAutoOrder.html")
	public ModelAndView autoOrder(){
		ModelAndView mv = new ModelAndView("bigGameOrder");
		
		List<BigGameOrderTableRecord> partners = partnerManager.findAllByAutoOrder();
		
		mv.addObject("partners", partners);
		mv.addObject("orderForm", new OrderForm());
		mv.addObject("currentOrder", tempsManager.getByKey(Temps.BIG_GAME_ORDER).getNumeric());
		
		return mv;
	}
	
	@RequestMapping(value="/bigGameOrderSave.html")
	public ModelAndView save(
			@ModelAttribute(value="orderForm") OrderForm orderForm
			){
		ModelAndView mv = new ModelAndView("redirect:index.html");
		
		mv.addObject(Constants.SCREEN, "bigGameOrder.html");
		partnerManager.saveOrder(orderForm.getIds(),true, false);
		
		return mv;
	}
}
